import { Injectable } from '@nestjs/common';
import { Coffee } from './entities/coffee.entity';
import { CreateCoffeeDto } from './dto/create-coffee.dto';

@Injectable()
export class CoffeesService {
  private coffees: Coffee[] = [];
  private nextId = 1;

  create(createCoffeeDto: CreateCoffeeDto): Coffee {
    const newCoffee: Coffee = {
      id: this.nextId++,
      name: createCoffeeDto.name,
      flavors: createCoffeeDto.flavors,
      description: createCoffeeDto.description,
      createdAt: new Date(),
    };
    this.coffees.push(newCoffee);
    return newCoffee;
  }

  findAll(): Coffee[] {
    return this.coffees;
  }

  findOne(id: number): Coffee | undefined {
    return this.coffees.find((coffee) => coffee.id === id);
  }

  recommendForCat(catName: string): Coffee {
    // 根据猫咪名字推荐咖啡
    const flavors = this.getFlavorsBasedOnCatName(catName);
    return {
      id: 0, // 临时ID，因为是推荐不是实际创建
      name: `专为${catName}推荐的咖啡`,
      flavors,
      description: `这款咖啡是根据${catName}的个性特别推荐的!`,
      createdAt: new Date(),
    };
  }

  private getFlavorsBasedOnCatName(name: string): string[] {
    // 简单逻辑：根据名字长度决定风味
    const length = name.length;
    if (length < 5) return ['清淡', '柔和'];
    if (length < 8) return ['中等', '平衡'];
    return ['浓郁', '复杂'];
  }
}
